package cn.com.fuhuitong.main.cart.adapter

import android.content.Context
import android.util.SparseBooleanArray
import android.view.View
import android.widget.CheckBox
import androidx.core.util.remove
import androidx.lifecycle.MutableLiveData
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.cart.entity.CartListEntity
import cn.com.fuhuitong.main.cart.ui.fragment.CartFragment
import cn.com.fuhuitong.utils.SpanUtils
import com.alibaba.android.vlayout.LayoutHelper
import com.jakewharton.rxbinding2.view.RxView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.http.HttpResponse
import cn.com.fuhuitong.main.cart.entity.CartListResponse
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.item_cart_goods.view.*
import java.util.concurrent.TimeUnit


enum class CVT(value: Int) {
    CART_GOODS(1),
    CART_BOTTOM(2)
}

class CartGoodsAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<CartListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_cart_goods,
    mItemViewType = CVT.CART_GOODS.ordinal
) {

    init {
        addChildClickViewIds(R.id.checkbox_goods_single)
    }

    private val dispose: CompositeDisposable = CompositeDisposable()

    val cartListDataLiveData = MutableLiveData<MutableList<CartListEntity>>()

    private val sbArray = SparseBooleanArray()

    override fun onBindViewHolder(itemView: View, position: Int, itemData: CartListEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        itemView.text_goods_name.text = itemData.title
//        itemView.text_goods_price.text =
//            SpanUtils.setMoneyStyle(if (itemData.is_specifications == 1) itemData.specifications_price.toDouble() else itemData.price.toDouble())
        itemView.text_goods_price.text =
            SpanUtils.setMoneyStyle(itemData.price.toDouble())
        itemView.text_goods_num.text = itemData.num.toString()

        val plusDisposable: Disposable = RxView.clicks(itemView.text_goods_plus)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe {
                setOnItemChildClick(itemView.text_goods_plus, position, itemData)
            }
        dispose.delete(plusDisposable)
        dispose.add(plusDisposable)

        val reduceDisposable: Disposable = RxView.clicks(itemView.text_goods_reduce)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe {
                setOnItemChildClick(itemView.text_goods_reduce, position, itemData)
            }
        dispose.delete(reduceDisposable)
        dispose.add(reduceDisposable)

        val deleteDisposable: Disposable = RxView.clicks(itemView.text_goods_delete)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribeOn(AndroidSchedulers.mainThread())
            .subscribe {
                setOnItemChildClick(itemView.text_goods_delete, position, itemData)
            }
        dispose.delete(deleteDisposable)
        dispose.add(deleteDisposable)

        itemView.checkbox_goods_single.isChecked = isSelectedItem(position)

        itemView.tag = position
    }

    override fun setAddDataNew(data: MutableList<CartListEntity>) {
        if (this.data.size == 0) {
            super.setAddDataNew(data)
        } else {
            data.removeAll(this.data)
            this.data.addAll(data)
            this.notifyDataSetChanged()
        }

        this.postValue()
    }

    fun upDataItem(action: CartFragment.Action, position: Int) {
        val entity = this.data[position]
        entity.num = if (action == CartFragment.Action.PLUS)
            entity.num + 1 else entity.num - 1
        this.notifyItemChanged(position)
    }

    fun deleteItem(position: Int) {
        this.data.removeAt(position)
        this.sbArray.remove(position, true)
        this.notifyDataSetChanged()
    }

    fun deleteItem(data: MutableList<CartListEntity>) {
        data.forEach {
            for ((index, entity) in this.data.withIndex()) if (it.id == entity.id) {
                this.sbArray.remove(index, true)
            }
        }
        this.data.removeAll(data)
        this.notifyDataSetChanged()
    }


    override fun setOnItemChildClick(v: View, position: Int, t: CartListEntity) {
        super.setOnItemChildClick(v, position, t)
        if (v.id == R.id.checkbox_goods_single) {
            val isChecked: Boolean = (v as CheckBox).isChecked
            this.setSelectedItem(position, isChecked)
            this.postValue()
        }
    }

    /**
     * 设置给定位置条目的选择状态
     * */
    private fun setSelectedItem(position: Int, isChecked: Boolean) {
        sbArray.put(position, isChecked)
    }

    /**
     * 根据位置判断条目是否选中
     * */
    private fun isSelectedItem(position: Int): Boolean {
        return sbArray.get(position)
    }

    /**
     * 设置是否全选
     * */
    fun setSelectedTotal(isTotal: Boolean) {
        for (i: Int in 0 until data.size) {
            setSelectedItem(i, isTotal)
        }
        this.notifyDataSetChanged()
        this.postValue()
    }

    /**
     * 获得选中条目的结果
     * */
    fun getSelectedItem(): MutableList<CartListEntity> {
        val selectList: MutableList<CartListEntity> = mutableListOf()
        for ((index: Int, data: CartListEntity) in data.withIndex()) {
            if (isSelectedItem(index)) selectList.add(data)
        }
        return selectList
    }

    fun postValue() {
        cartListDataLiveData.postValue(getSelectedItem())
    }
}

class CartBottomAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<MutableList<CartListEntity>>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_cart_bottom,
    mItemViewType = CVT.CART_BOTTOM.ordinal
) {

}